<!-- 需求 -->
<template>
	<view class="u-p-b-60">
		<view class="page-view">
			<u-form label-width="auto" :model="serviceForm" ref="serviceForm" :rules="rules" :error-type="['toast']">
				<u-form-item label-position="top" label="服务标题" prop="title">
					<u-input type="textarea" :border="true" v-model="serviceForm.title" placeholder="一句话描述您能提供的问题服务，如：xxx问题可找我"
						height="170" :clearable="false" :maxlength="30" />
				</u-form-item>
				<u-form-item label="所属行业" prop="class_two_name" required right-icon="arrow-right">
					<view @click="classPicker=true" class="u-flex-1 align-right">
						<text style="color: #c0c4cc;" v-if="!serviceForm.class_two_name">请选择</text>
						<text v-else>{{serviceForm.class_two_name}}</text>
					</view>
				</u-form-item>
				<u-form-item label="服务标签" prop="class_three_name" required right-icon="arrow-right">
					<view @click="opentagPicker" class="u-flex-1 align-right">
						<text style="color: #c0c4cc;" v-if="!serviceForm.class_three_name">请选择</text>
						<text v-else>{{serviceForm.class_three_name}}</text>
					</view>
				</u-form-item>
				<u-gap height="30" bg-color="#F6F6F6"></u-gap>
				<u-form-item label="定价" :border-bottom="false">
					<text slot="right" class="u-font-10">平台代开发票</text>
				</u-form-item>
				<view class="u-p-l-30 u-p-r-30">线下帮助（1对1服务）</view>
				<u-form-item :border-bottom="false" prop="price">
					<view class="input-box">
						<text class="u-font-28 u-tips-color">￥</text>
						<view class="u-line"></view>
						<u-input :placeholder="`${config.S_MIN_PRICE}~${config.S_MAX_PRICE}元`" class="u-flex-1" type="digit"
							v-model="serviceForm.price" @blur="priceChange" :clearable="false" />
					</view>
				</u-form-item>
				<view class="u-light-color u-p-b-30 u-p-l-30 ">实际收入：{{sPrice}}元 <text
						class="iconfont icon-tishi u-type-warning u-font-26 u-m-l-20" @click="rulePicker=true"></text></view>
				<u-gap height="30" bg-color="#F6F6F6"></u-gap>
				<u-form-item label-position="top" label="详细描述能提供帮助的内容" prop="description">
					<u-input type="textarea" :border="true" :placeholder="placeholder" height="354" :clearable="false"
						v-model="serviceForm.description" :auto-height="false" :maxlength="500" />
				</u-form-item>
			</u-form>
		</view>

		<view class="u-p-30">
			<u-button type="primary" hover-class="none" @click="submitHandle" :loading="loading">确定发布</u-button>
		</view>


		<!-- 行业 -->
		<u-picker mode="selector" :range="classList" v-model="classPicker" :default-selector="defaultClass" title="选择行业"
			:confirm-color="$appConfig.mainColor" @confirm="confirmClass" range-key="name"></u-picker>
		<!-- 标签 -->
		<u-popup v-model="tagsPicker" mode="bottom" border-radius="20" :mask-close-able="false"
			:safe-area-inset-bottom="true">
			<view class=" u-border-bottom u-flex u-row-between" style="padding: 25rpx 35rpx;">
				<text class="u-tips-color" @click="tagsPicker=false">取消</text>
				<view class="u-font-32 ">
					选择服务标签(多选)
				</view>
				<view class="u-type-primary" @click="comfimTag">确认</view>
			</view>
			<view class="tag-content">
				<view class="tag-item" :class="[selectTags.includes(item)?'active':'']" v-for="(item,index) in tagList"
					:key="index" @click="selectTagesHandle(index)">{{item.name}}</view>
			</view>
		</u-popup>

		<!-- 规则 -->
		<u-popup v-model="rulePicker" mode="bottom" border-radius="20" closeable :safe-area-inset-bottom="true">
			<view class=" u-border-bottom" style="padding: 25rpx 35rpx;">
				<view class="u-font-30 ">
					平台定价规则
				</view>
			</view>
			<view class="u-p-30">
				扣除手续费和税共10%
			</view>
		</u-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				placeholder: `详细描写您能提供帮助的内容。
				
					示例：
						包含xx服务； 
						服务时间周期； 
						备注说明：如涉及到咨询服务请写明个人建议仅作参考等。`,
				classPicker: false,
				tagsPicker: false,
				rulePicker: false,
				serviceForm: {
					title: '',
					price: '',
					description: '',
					class_two_name: '',
					class_two_id: '',
					class_three_name: '',
					class_three_id: ''
				},
				rules: {
					title: [{
						required: true,
						message: '请填写服务标题'
					}],
					price: [{
							required: true,
							transform(value) {
								return String(value)
							},
							message: '请填服务定价'
						},
						{
							validator: (rule, value, callback) => {
								return this.$u.test.amount(value);
							},
							message: '请填正确的金额数值'
						},
						{
							validator: (rule, value, callback) => {
								return this.$u.test.range(Number(value), [this.$appConfig.S_MIN_PRICE, this.$appConfig.S_MAX_PRICE])
							},
							message: `金额必须为${this.$appConfig.S_MIN_PRICE}~${this.$appConfig.S_MAX_PRICE}元以内`,
						}
					],
					description: [{
						required: true,
						message: '请填写服务内容'
					}],
					class_two_name: [{
						required: true,
						message: '请选择所属行业'
					}],
					class_three_name: [{
						required: true,
						message: '请至少选择一个服务标签'
					}],
				},
				tagList: [],
				classList: [],
				defaultClass: [0],
				formID: '',
				selectTags: [],
				loading: false,
				sPrice: '0.00',
				ruleText: '',
				config: this.$appConfig
			}
		},
		onReady() {
			this.$refs.serviceForm.setRules(this.rules)
		},
		onLoad(e) {
			const userInfo = this.$store.state.vuex_userInfo;
			if (e.id) {
				this.formID = e.id;
				this.queryServiceDetail()
			} else {
				this.queryTages(userInfo.class_one_id || 0, true)
			}
		},
		methods: {
			// 查询行业 / 标签
			queryTages(pid, isOne = false) {
				this.$u.api.public.industry_class({
					pid
				}).then(res => {
					if (isOne) {
						this.classList = res;
						const index = this.serviceForm.class_two_id ? res.findIndex(item => item.id == this.serviceForm
							.class_two_id) : 0
						this.defaultClass = [index]
						console.log(this.defaultClass);
					}
					this.tagList = res
				})
			},
			// 价格计算
			complatePrice() {
				this.$u.api.compulatePrice({
					price: this.serviceForm.price
				}).then(res => {
					this.sPrice = res.real_price
				})
			},
			priceChange(value) {
				if (!this.$u.test.range(Number(value), [this.$appConfig.S_MIN_PRICE, this.$appConfig.S_MAX_PRICE])) {
					this.sPrice = '0.00'
					this.$u.toast(`金额必须为${this.$appConfig.S_MIN_PRICE}~${this.$appConfig.S_MAX_PRICE}元以内`)
				} else {
					this.complatePrice()
				}
			},
			queryServiceDetail() {
				this.$u.api.serveDetail(this.formID).then(res => {
					const keys = Object.keys(this.serviceForm);
					keys.map(key => {
						this.$set(this.serviceForm, key, res[key])
					})

					this.queryTages(res.class_one_id || 0, true)
				})
			},
			submitHandle() {
				if (this.formID) {
					this.editService()
				} else {
					this.addService()
				}
			},
			addService() {
				this.$refs.serviceForm.validate(valid => {
					if (valid) {
						this.loading = true
						this.$u.api.serveAdd(this.serviceForm).then(res => {
							uni.showModal({
								content: '服务已提交审核，可前往【我的服务】查看状态',
								confirmText: '前往查看',
								confirmColor: this.$appConfig.mainColor,
								success: (e) => {
									if (e.confirm) {
										uni.redirectTo({
											url: '/pages/subpage/profile/my_producte?status=2'
										})
									} else {
										uni.navigateBack()
									}
								}
							})
						}).finally(() => {
							this.loading = false
						})
					} else {
						console.log('不通过');
					}
				})
			},
			editService() {
				this.$refs.serviceForm.validate(valid => {
					if (valid) {
						this.loading = true
						this.$u.api.serveEdit(this.formID, this.serviceForm).then(res => {
							this.$u.toast('已重新提交审核')
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)

						}).finally(() => {
							this.loading = false
						})
					} else {
						console.log('不通过');
					}
				})
			},
			confirmClass(e) {
				this.defaultClass = e;
				const clas = this.classList[e[0]]
				this.serviceForm.class_two_id = clas.id;
				this.serviceForm.class_two_name = clas.name
				this.serviceForm.class_three_name = '';
				this.selectTags = [];
				this.queryTages(clas.id)
			},
			// 打开并选标签
			opentagPicker() {
				if (this.serviceForm.class_two_id) {
					this.tagsPicker = true
				} else {
					this.$u.toast("请选择所属行业")
				}
			},
			comfimTag(e) {
				this.serviceForm.class_three_id = this.selectTags.map(item => item.id).join(',')
				this.serviceForm.class_three_name = this.selectTags.map(item => item.name).join(',')
				this.tagsPicker = false
			},
			selectTagesHandle(index) {
				const tag = this.tagList[index];
				const tindex = this.selectTags.findIndex(item => item.id == tag.id)
				if (tindex >= 0) {
					this.selectTags.splice(tindex, 1)
				} else {
					this.selectTags.push(tag)
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.page-view {
		margin-top: 30rpx;
		background-color: #fff;
	}

	.page-view ::v-deep .u-form-item {
		margin: 0 30rpx;
	}

	.input-box {
		height: 88rpx;
		background: #F6F6F6;
		border-radius: 60rpx 60rpx 60rpx 60rpx;
		padding: 0 32rpx;
		display: flex;
		align-items: center;
	}

	.u-line {
		width: 0;
		height: 42rpx;
		border-left: 1px solid #D9D9D9;
		margin: 0 20rpx;
	}

	.tag-content {
		padding: 30rpx 40rpx 80rpx;
		display: flex;
		flex-wrap: wrap;

		.tag-item {
			padding: 10rpx 20rpx;
			border: 1rpx solid #D9D9D9;
			color: #666666;
			border-radius: 10rpx;
			margin: 10rpx;

			&.active {
				color: $u-type-primary;
				border-color: $u-type-primary;
			}
		}
	}
</style>